﻿using System;
using System.Text;
using System.Web;
using System.Web.Script.Serialization;
using TSB.SPEventReceiverManager.Entities;

namespace TSB.SPEventReceiverManager.Layouts.TSBLayouts
{
    public partial class EditEventReceiver : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            var cmd = context.Request.QueryString["cmd"] ?? string.Empty;
            var listId = Guid.Empty;
            try
            {
                listId = context.Request.QueryString["listId"] != null ? new Guid(context.Request.QueryString["listId"]) : Guid.Empty;
            }
            catch
            {
                listId = Guid.Empty;
            }

            var receiverId = Guid.Empty;
            try
            {
                receiverId = context.Request.QueryString["receiverId"] != null ? new Guid(context.Request.QueryString["receiverId"]) : Guid.Empty;
            }
            catch
            {
                receiverId = Guid.Empty;
            }

            if (string.IsNullOrEmpty(cmd) || listId == Guid.Empty)
            {
                ;//
            }
            
            var jsonResponse = new JsonResponse();

            switch (cmd)
            {
                case "delete":
                    if (receiverId == Guid.Empty)
                    {
                        jsonResponse.status = "missing receiverId";
                    }
                    else
                    {
                        jsonResponse = DeleteCmd(listId, receiverId);
                    }
                    break;
                default:
                    jsonResponse.status = "invalid cmd";
                    break;
            }


            var serializer = new JavaScriptSerializer();
            var sbJsonResults = new StringBuilder();
            serializer.Serialize(jsonResponse, sbJsonResults);

            context.Response.Clear();
            context.Response.ContentType = "application/json; charset=utf-8";
            context.Response.Write(sbJsonResults.ToString());
        }

        private JsonResponse DeleteCmd(Guid listId, Guid receiverId)
        {
            var repository = new EventReceiverRepository();

            return new JsonResponse
                                   {
                                       status = repository.DeleteEventReceiver(listId, receiverId).ToString()
                                   };
        }

        private JsonResponse AddCmd(Guid listId, string receiverType, string assemblyName, string className)
        {
            var repository = new EventReceiverRepository();
            //NB Still to write this functionality
            //var eventreceivertype = (speventreceivertype) enum.parse(typeof (speventreceivertype), receivertype);

            //return new jsonresponse
            //           {
            //               status = repository.addeventreceiver(listid, eventreceivertype, assemblyname, classname).tostring()
            //};

            return new JsonResponse{status = "not set"};
        }
    }
}
